home *** CD-ROM | disk | FTP | other *** search
- Path: gate.net!pslfl2-18
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: my atoi function, could someone suggest...
- Date: 4 Jan 1996 16:14:53 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4cgudt$1e9i@news.gate.net>
- References: <4cf7ap$q4u@kaleka.seanet.com>
- NNTP-Posting-Host: pslfl2-18.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4cf7ap$q4u@kaleka.seanet.com>,
- mitchell@seanet.com (where am i?) wrote:
- >Hello. This is my first attempt at an atoi function. Although it
- >works and behaves just like atoi, I was wondering if anyone would care
- >for a look and make suggestions back to me about its efficiency.
- >
- >int atoi ( char *change )
- >{
- > int newint = 0, sign = 1;
- >
- > while ( *change )
- > {
- > if ( (*change < '0' || *change > '9') && *change != '-' )
-
- if (!isdigit(*change) && *change != '-')
-
- If I were to assume you didn't want to use library functions or macros in this
- function, I wouldn't say anything about this line. Other than that, you could
- use isdigit() (#include <ctype.h>) which can use a lookup table and should be
- faster.
-
- > {
- > *change++;
-
- change++;
-
- You don't need indirection here.
-
- > }
- > else
- > {
- > if ( *change == '-' )
- > {
- > *change++;
-
- change++;
-
- > if ( *change >= '0' && *change <= '9' )
-
- if(isdigit(*change))
-
- > sign = -1;
- > }
- > if ( *change >= '0' && *change <= '9' )
-
- if(isdigit(*change))
-
- > {
- > while ( sign )
- > {
- > newint *= 10;
- > newint = newint + *change - 48;
-
- newint = newint + *change - '0';
-
- Just in case '0' != 48.
-
- > *change++;
-
- change++;
-
- > if ( *change < '0' || *change > '9' )
-
- if (!isdigit(*change))
-
- > {
- > return ( newint * sign );
- > exit (0);
-
- exit() should be omitted.
-
- > }
- > }
- > }
- > }
- > }
- > return ( 0 );
- >}
- >
- >Thank you all very much.
- >
- >Mitch
- >
-
-
- "Whatcha got on?...Your mind?"
-